package feng.demo.controller.adm;

import cn.dev33.satoken.stp.StpUtil;
import feng.demo.entity.Commodity;
import feng.demo.entity.Remark;
import feng.demo.entity.User;
import feng.demo.entity.XJson;
import feng.demo.service.CommodityService;
import feng.demo.service.RemarkService;
import feng.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adm/remark")
public class AdmRemarkController {

    @Autowired
    RemarkService remarkService;
    @Autowired
    CommodityService commodityService;
    @Autowired
    UserService userService;
    @GetMapping("/findAll/{p}/{l}")
    public String findAll(@PathVariable("p")Integer p,@PathVariable("l")Integer l){
        Map<String,Object> map=new HashMap<>();
        int count = remarkService.findCount();
        List<Remark> list = remarkService.findByAllLimit(p, l);
        map.put("count",count);
        map.put("remark",list);
        return new XJson(200,"",map).toString();
    }

    @GetMapping("/del/{id}/{p}/{l}")
    public String del(@PathVariable("id")Integer remid,@PathVariable("p")Integer p,@PathVariable("l")Integer l){
        remarkService.del(remid);
        Map<String,Object> map=new HashMap<>();
        int count = remarkService.findCount();
        List<Remark> list = remarkService.findByAllLimit(p, l);
        map.put("count",count);
        map.put("remark",list);
        return new XJson(200,"删除成功",map).toString();
    }


    @GetMapping("/findByid/{id}")
    public String findByid(@PathVariable("id")Integer remid){
        Map<String,Object> map=new HashMap<>();
        Remark remark = remarkService.findById(remid);
        List<Commodity> list = commodityService.findAll();
        map.put("remark",remark);
        map.put("coms",list);
        return new XJson(200,"",map).toString();
    }

    @GetMapping("/GoUpdate")
    public String goupdate(){
        List<Commodity> list = commodityService.findAll();
        return new XJson(200,"",list).toString();
    }

    @PostMapping("/add")
    public String add(Remark remark){
        Object phone = StpUtil.getLoginId();
        User user = userService.findByPhone(String.valueOf(phone));
        remark.setUser(user);
        remarkService.add(remark);

        return new XJson(200,"添加成功","").toString();
    }
    @PostMapping("/update")
    public String update(Remark remark){
        Object phone = StpUtil.getLoginId();
        User user = userService.findByPhone(String.valueOf(phone));
        remark.setUser(user);
        remarkService.update(remark);
        return new XJson(200,"更新成功","").toString();
    }
}
